package com.ant.storage.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;

/**
 * @author hzd
 * @version 1.0
 * @date 2022/3/16 9:15
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Value("${file.upload-path}")
    private String uploadPath;

    @Resource
    private  TokenInterceptor tokenInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        super.addResourceHandlers(registry);
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/pic/**").addResourceLocations("file:///" + uploadPath);
    }

    /**
     * @return 登录验证拦截器
     * 自定义登录验证拦截器
     */
    @Bean
    public TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor) // 添加拦截器
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns("/",
                        "/storageApi/pic/**", "/**/*.html", "/storageApi/doc.html",
                        "/storageApi/DingDing/**",
                        "/storageApi/webjars/**",
                        "/storageApi/login/**",
                        "/storageApi/uploadFile/**",
                        "/storageApi/Images/storage/**");    //对应的不拦截的请求       "/storageApi/cashbook/getBalance",
        super.addInterceptors(registry);
    }

    /**
     * 跨域处理
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        super.addCorsMappings(registry);
        registry.addMapping("/**").
                allowedOriginPatterns("*").// 设置允许跨域请求的域名
                // allowedOrigins("*"). // 将.allowedOrigins替换成.allowedOriginPatterns即可。
                allowedMethods("*"). //允许任何方法（post、get等）
                allowedHeaders("*"). //允许任何请求头
                allowCredentials(true). //带上cookie信
                maxAge(3600L);  // 跨域允许时间
        //  exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内，不需要再发送预检验请求，可以缓存该结果
    }


}
